package com.casic.modules.jy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.modules.jy.entity.*;
import com.casic.modules.jy.service.*;
import com.casic.modules.sys.service.SysUserService;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 学生考试答题明细表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jysjxssublist")
public class JySjXsSublistController {
	@Autowired
	private JySjXsSublistService jySjXsSublistService;
	@Autowired
	private JySjOptionService jySjOptionService;
	@Autowired
	private JySjXsService jySjXsService;
	@Autowired
	private JySjTitleService jySjTitleService;
	@Autowired
	private JyKcxxService  jyKcxxService;
	@Autowired
	private JySjInfoService jySjInfoService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JySjXsSublistEntity> page=jySjXsSublistService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") String id){
		JySjXsSublistEntity jySjXsSublist = jySjXsSublistService.findById(id);
		return R.ok().put("jySjXsSublist", jySjXsSublist);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody JySjXsSublistEntity jySjXsSublist){
		jySjXsSublistService.save(jySjXsSublist);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody JySjXsSublistEntity jySjXsSublist){
		jySjXsSublistService.edit(jySjXsSublist);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] ids){
		jySjXsSublistService.removeByIds(ids);
		return R.ok();
	}

	@RequestMapping("/bathSave")
	public R bathSave(@RequestBody String jySjXsSublist){
		List<JySjXsSublistEntity> list = JSON.parseArray(jySjXsSublist,JySjXsSublistEntity.class);
		if(list !=null && list.size()>0){
			jySjXsSublistService.saveBatch(list);
		}
		return R.ok();
	}

	@RequestMapping("/queryAnswer/{xsId}")
	public R queryAnswer(@PathVariable("xsId") String xsId){

		JySjXsEntity xs = jySjXsService.findById(xsId);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("infoId",xs.getSjId());
		List<JySjTitleEntity> Titlelist = jySjTitleService.findList(params);
		if(Titlelist !=null && Titlelist.size()>0){
			for(JySjTitleEntity title : Titlelist){
				params.put("titleCode",title.getId());
				List<JySjOptionEntity> optionList = jySjOptionService.findList(params);
				title.setOptionList(optionList);
				params.put("xsId",xsId);
				params.put("titleId",title.getId());
				title.setAnswerList(jySjXsSublistService.findList(params));
			}
		}
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("xm",xs.getCandidate());
		data.put("list",Titlelist);
		JySjInfoEntity info = jySjInfoService.findById(xs.getSjId());
		JyKcxxEntity kc = jyKcxxService.findById(info.getKcId());
		data.put("km",kc);
		data.put("deptName",xs.getDeptName());
		data.put("sj",info.getName());
		return R.ok().put("list",data);
	}


	@RequestMapping("/bathUpdate")
	public R bathUpdate(@RequestBody String jsonBody){
		List<JySjXsSublistEntity> list = null;
		if(StringUtil.isNotEmpty(jsonBody)){
			list = JSON.parseArray(jsonBody,JySjXsSublistEntity.class);
		}else{
			return R.error("参数为空");
		}
		if(list !=null && list.size()>0){
			for(JySjXsSublistEntity entity : list){
				jySjXsSublistService.edit(entity);
			}
		}
		return R.ok();
	}



}
